﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using System.Data;


namespace HRM
{
    public partial class frmUNCInfo : Form
    {
        DialogResult _dlgResult;
        int _UNCID;
        UNCClient _wcfUNC;
        UNCInfo _UNCInfo;
        RoleRightInfo _ListRole;
        bool isChange;
        public frmUNCInfo(int UNCID, RoleRightInfo ListRole)
        {
            InitializeComponent();
           
            LoadBranch();
            _UNCInfo = new UNCInfo();
            _wcfUNC = Service.UNC();
            _dlgResult = DialogResult.Cancel;
            _UNCID = UNCID;
            this.KeyPreview = true;
            if (_UNCID > 0)
            {
                _UNCInfo = _wcfUNC.UNC_GetInfo(_UNCID);
                Info2Form();
            }
            _ListRole = ListRole;
        }
        private void LoadBranch()
        {
            BranchClient wcf = Service.Branch();

            DataTable dt;
            if (!Program.isAdmin && !Program.isViewAll)
            {
                dt = wcf.Branch_GetOne(Program.intBranchID).Tables[0];
            }
            else
            {
                dt = wcf.Branch_GetAll().Tables[0];
            }               
              
            cbBranchCode.DisplayMember = "Branch_Name";
            cbBranchCode.ValueMember = "Branch_Code";
            cbBranchCode.DataSource = dt;
        }

        private void Info2Form()
        {
            txtNo.Text = _UNCInfo.No;
            txtMonth.Text = _UNCInfo.Month.ToString();
            txtContent.Text = _UNCInfo.Content;
            cbBranchCode.SelectedValue = _UNCInfo.BranchCode;
            txtYear.Text = _UNCInfo.Year.ToString();
            txtAmount.Text = _UNCInfo.Amount.ToString("#,##0");
            txtQuater.Text = _UNCInfo.Quater.ToString();
            txtDescription.Text = _UNCInfo.Discription;
            mskDate.Text = _UNCInfo.Date.ToString("dd/MM/yyyy");
            isChange = false;
        }
        private void form2Info()
        {
            _UNCInfo.Date = UISetting.GetDate(mskDate.Text);
            _UNCInfo.Month = Convert.ToInt32(txtMonth.Text);
            _UNCInfo.Year = Convert.ToInt32(txtYear.Text);
            _UNCInfo.Content = txtContent.Text;
            _UNCInfo.Amount = Convert.ToDecimal(txtAmount.Text);
            _UNCInfo.Discription = txtDescription.Text;
            _UNCInfo.BranchCode = Program.strBranchBHXH;
            _UNCInfo.Quater = txtQuater.Text;
            _UNCInfo.No = txtNo.Text;
            _UNCInfo.ID = _UNCID;

        }
     
        bool CheckValid()
        {

            if (!CheckTextValid.IsInteger(txtMonth.Text))
            {
                
                    txtMonth.Focus();
                    MessageBox.Show("Tháng không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                
            }
            if (!CheckTextValid.IsInteger(txtYear.Text))
            {
                txtYear.Focus();
                MessageBox.Show("Năm không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }            

            if (!CheckTextValid.IsDecimal(txtAmount.Text))
            {
                txtAmount.Focus();
                MessageBox.Show("Số tiền không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtNo.Text =="")
            {
                txtNo.Focus();
                MessageBox.Show("Số phiếu không được để trống", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;

        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!_ListRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_UNCInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _UNCID = 0;
            _UNCInfo = new UNCInfo();
            txtYear.Text = "";
            txtMonth.Text = "";
            txtContent.Text = "";
            txtAmount.Text = "";
            txtDescription.Text = "";
            mskDate.Text = "";
            txtNo.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }


            form2Info();
            if (_UNCID > 0)//Update
            {
                if (!_ListRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (_wcfUNC.UNC_Update(_UNCInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    _dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else //insert
            {
                if (!_ListRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                _UNCID = _wcfUNC.UNC_Insert(_UNCInfo);
                if (_UNCID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    _dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void frmUNCInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F4:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
            }
        }
     
        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void txtUNC_Leave(object sender, EventArgs e)
        {

        }

        private void frmUNCInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfUNC.Close();

            this.DialogResult = _dlgResult;
        }

        

    }
}